package com.xiaoge.resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * TODO
 *
 * @author <a href="mailto:1330137071@qq.com">Zhang Xiao</a>
 * @since
 */
@Component
public class TestResource {

	@Autowired
	private ApplicationContext applicationContext;


	/**
	 * todo 资源加载
	 * @throws Exception
	 */
	public void test() throws Exception {
		// todo 加载资源
		Resource resource1 = applicationContext.getResource("https://www.baidu.com");
		byte[] bytes = new byte[10000];
		resource1.getInputStream().read(bytes);
		System.out.println(Arrays.asList(new String(bytes).split("\r\n")));
		System.out.println(resource1.contentLength());
		System.out.println(resource1.getDescription());

		System.out.println("--------------------------------------------------------");

		// todo 类路径下-资源通配符
		Resource[] resources2 = applicationContext.getResources("classpath:com\\xiaoge\\service\\User*");
		for (Resource resource : resources2) {
			System.out.println(resource.getFilename());
			System.out.println(resource.contentLength());
		}

		System.out.println("--------------------------------------------------------");

		// todo 类路径包含jar内资源-资源通配符
		Resource[] resources3 = applicationContext.getResources("classpath*:META-INF/spring.factories");
		for (Resource resource : resources3) {
			System.out.println(resource.getFilename());
			System.out.println(resource.contentLength());

		}

		System.out.println("--------------------------------------------------------");

		// todo 加载磁盘路径
		Resource[] resources4 = applicationContext.getResources("file:C:\\Users\\13301\\Desktop\\5.30-6.16");
		for (Resource resource : resources4) {
			System.out.println(resource.getFilename());
			System.out.println(resource.contentLength());
		}
	}

}
